package com.web.daoimpl;

import com.web.dao.GradeDao;
import com.web.entity.Grade;
import com.web.util.DatabaseUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author 12270
 */
public class GradeDaoImpl extends BaseDaoImpl implements GradeDao{

    @Override
    public List<Grade> findOneStudentGradeBySid(String sid) {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Grade> list = new ArrayList<>();

        try {
            con = DatabaseUtil.getConnection();
            ps = con.prepareStatement("select * from Grades where sid=?");
            ps.setString(1, sid);
            rs = ps.executeQuery();
            while (rs.next()) {
                list.add(new Grade(rs.getString(1), rs.getString(2), rs.getString(3),rs.getInt(4)));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }
    return list;
}

    @Override
    public List<Grade> findClassGrade(String className, String courseName) {
        Connection con = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<Grade> list = new ArrayList<>();

        try {
            con = DatabaseUtil.getConnection();
            ps = con.prepareStatement("select sid,grade from Grades where className=? and courseName=?");
            ps.setString(1, className);
            ps.setString(2, courseName);
            rs = ps.executeQuery();
            while (rs.next()) {
                list.add(new Grade(rs.getString(1),rs.getInt(2)));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DatabaseUtil.close(rs, ps, con);
        }
    return list;
    }

    @Override
    public int InsertStudentGrade(Grade g) {
        return excuteUpdate_o("insert into Grades values(?,?,?,?)",g.getSid(),g.getCourseName(),g.getClassName(),g.getGrade());
    }

}
